//
//  BBSimpleEventView.m
//  BBTouch
//
//  Created by ben smith on 5/30/08.
//  This file is part of BBTouch.
//
//  BBTouch is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  BBTouch is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public License
//  along with BBTouch.  If not, see <http://www.gnu.org/licenses/>.
// 
//  Copyright 2008 Ben Britten Smith ben@benbritten.com .
//

#import "BBSimpleEventView.h"
#import "BBBlobEvent.h"
#import "BBConfigurationController.h"


@implementation BBSimpleEventView
-(void)awakeFromNib
{
	blobPoints = [[NSMutableArray alloc] init];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(blobDown:) name:@"BBBlobDown" object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(blobUp:) name:@"BBBlobUp" object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(blobDragged:) name:@"BBBlobDragged" object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(blobDone:) name:@"BBBlobMovementFinished" object:nil];
}

- (void)drawRect:(NSRect)rect 
{	
  // Drawing code here.
	NSEnumerator * blobEnum = [blobPoints objectEnumerator];
	BBBlobEvent * anEvent;
	while ((anEvent = [blobEnum nextObject])) {
		[self drawBlobEvent:anEvent];
	}
}


-(void)drawBlobEvent:(BBBlobEvent*)theBlobEvent
{
	float radius = [theBlobEvent radius];
	NSPoint bLoc = [theBlobEvent screenLocation];
	// blob location is in a normalizes coordinate, so from 0.0 - 1.0
	// need to scale it out to match my bounds
	// this is the simple method, but does not preserve aspect ratio
	bLoc.x *= [self bounds].size.width;
	bLoc.y *= [self bounds].size.height;
	NSRect blobRect = NSMakeRect(bLoc.x - radius, bLoc.y - radius, radius * 2.0, radius * 2.0);
	// make a pretty oval in a rectangle. oooh!
	[[NSColor greenColor] set];
	[NSBezierPath strokeRect:blobRect];
	[[NSColor purpleColor] set];
	[[NSBezierPath bezierPathWithOvalInRect:blobRect] stroke];
}

///////////////////////////////////////////////
// blob notification methods
// TODO: add some checks to make sure that I am getting proper blob objects and not
// being called from somewhere that might not be a blob event

-(void)blobDown:(NSNotification*)note
{
	// add the blob to my list
	[blobPoints addObject:[note object]];
}

-(void)blobUp:(NSNotification*)note
{
	int blobIndex = [self indexOfBlobWithID:[[note object] blobID]];
	if (blobIndex != NSNotFound) {
		[blobPoints removeObjectAtIndex:blobIndex];
	}
}

-(void)blobDragged:(NSNotification*)note
{
	// replace the old blob with the new blob info
	int blobIndex = [self indexOfBlobWithID:[[note object] blobID]];
	if (blobIndex != NSNotFound) {
		[blobPoints replaceObjectAtIndex:blobIndex withObject:[note object]];
	}
}


// quick convenience method to grab blob indexes
-(int)indexOfBlobWithID:(int)blobId
{
	int blobIndex;
	for (blobIndex = 0; blobIndex < [blobPoints count]; blobIndex++) 
		if ([[blobPoints objectAtIndex:blobIndex] blobID] == blobId) return blobIndex;
	return NSNotFound;	
}

-(void)blobDone:(NSNotification*)note
{
	[self setNeedsDisplay:YES];
}


@end
